﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace Frame
{
    public class ApplicationManager
    {
        private string m_AppFilePath = System.Windows.Forms.Application.StartupPath + @"\App\AppFile.xml";
        private XmlDocument m_xmlDoc;
        private List<AppClass> m_Application;
        public ApplicationManager(string AppFilePath = "")
        {
            m_Application = new List<AppClass>();
            if(AppFilePath != "")
                this.m_AppFilePath = AppFilePath;
            LoadXml();                             
        }

        public List<AppClass> Application { set { m_Application = value; } get { return m_Application; } }

        public string AppFilePath
        {
            set { m_AppFilePath = value; }
            get { return m_AppFilePath; }
        }

        public int LoadXml()
        {
            if (m_xmlDoc == null)
            {
                m_xmlDoc = new XmlDocument();
            }
            m_xmlDoc.Load(m_AppFilePath);
            if (!File.Exists(AppFilePath)) return  2;
            XmlNode rootNode = m_xmlDoc.SelectSingleNode("Application");
            if (rootNode == null) return 0;
            XmlNodeList appClassNodes = rootNode.ChildNodes;
            if (appClassNodes.Count == 0)
            {
                return 1;
            } 
            foreach(XmlNode appClassNode in appClassNodes)
            {
                if(appClassNode.Name.CompareTo("AppClass") == 0)
                {
                    string name = appClassNode.SelectSingleNode("Name").InnerText;
                    bool expand = appClassNode.Attributes["expand"].Value.ToString().CompareTo("True") == 0;
                    XmlNodeList appNodes = appClassNode.ChildNodes;
                    AppClass appClass = new AppClass(name, expand);
                    appClass.image = appClassNode.SelectSingleNode("Image").InnerText; ;
                    foreach(XmlNode appNode in appNodes)
                    {
                        if (appNode.Name.CompareTo("App") == 0)
                        {
                            name = appNode.SelectSingleNode("Name").InnerText;
                            bool fav = appNode.Attributes["Favorite"].Value.ToString().CompareTo("True") == 0;
                            string path = appNode.SelectSingleNode("Path").InnerText.ToString();
                            App app = new App(name, path, fav);
                            appClass.AppList.Add(app);
                        } 
                    }
                    Application.Add(appClass);
                }
            }

            return 0;
        }

        public int SaveXml()
        {
            if (m_xmlDoc == null)
            {
                m_xmlDoc = new XmlDocument();
            }
            m_xmlDoc.Load(m_AppFilePath);
            XmlNode rootNode = m_xmlDoc.SelectSingleNode("Application");
            if (rootNode == null) return 1;
            rootNode.RemoveAll();
            foreach (AppClass appClass in Application)
            {
                XmlElement appClassEle = m_xmlDoc.CreateElement("AppClass");
                XmlElement appCalssNameEle = m_xmlDoc.CreateElement("Name");
                XmlElement appCalssImageEle = m_xmlDoc.CreateElement("Image");
                appClassEle.SetAttribute("expand", appClass.Expand.ToString());
                appCalssNameEle.InnerText = appClass.Name;
                appCalssImageEle.InnerText = appClass.image;
                appClassEle.AppendChild(appCalssNameEle);
                appClassEle.AppendChild(appCalssImageEle);
                rootNode.AppendChild(appClassEle);
                foreach (App app in appClass.AppList)
                {
                    XmlElement appEle = m_xmlDoc.CreateElement("App");
                    appEle.SetAttribute("Favorite", app.Favorite.ToString());
                    XmlElement appNameEle = m_xmlDoc.CreateElement("Name");
                    appNameEle.InnerText = app.Name;
                    XmlElement appPathEle = m_xmlDoc.CreateElement("Path");
                    appPathEle.InnerText = app.Path;
                    appEle.AppendChild(appNameEle);
                    appEle.AppendChild(appPathEle);
                    appClassEle.AppendChild(appEle);
                }
            }
            m_xmlDoc.Save(AppFilePath);
            return 0;
        }

        public AppClass GetClass(string className)
        {
            AppClass appClass = null;
            foreach (AppClass _appClass in Application)
            {
                if (_appClass.Name.CompareTo(className) == 0)
                {
                    appClass = _appClass;
                    break;
                }
            }
            return appClass;
        }

        /// <summary>
        /// 获取指定类的APP列表
        /// </summary>
        /// <param name="className">类名</param>
        /// <returns>应用列表</returns>
        public List<App> GetAppList(string className)
        {
            List<App> appList = new List<App>();
            foreach (AppClass appClass in Application)
            {
                if (appClass.Name.CompareTo(className) == 0)
                {
                    appList = appClass.AppList;
                    break;
                }
            }
            return appList;
        }
        /// <summary>
        /// 通过APP的名称获取所在的类
        /// </summary>
        /// <param name="appName">APP的名称</param>
        /// <returns>应用分类</returns>
        public AppClass AppToClass(string appName)
        {
            AppClass appClass = null;
            foreach (AppClass _appClass in Application)
            {
                foreach (App app in _appClass.AppList)
                {
                    if (app.Name.CompareTo(appName) == 0)
                    {
                        appClass = _appClass;
                        break;
                    }
                }
            }
            return appClass;
        }

        public App GetApp(string appName)
        {
            App app = null;
            foreach (AppClass _appClass in Application)
            {
                foreach (App _app in _appClass.AppList)
                {
                    if (_app.Name.CompareTo(appName) == 0)
                    {
                        app = _app;
                        break;
                    }
                }
            }
            return app;
        }

        public int RunApp(string appName)
        {
            foreach (AppClass _appClass in Application)
            {
                foreach (App app in _appClass.AppList)
                {
                    if (app.Name.CompareTo(appName) == 0)
                    {
                        if (File.Exists(app.Path))
                        {
                            Frame.DirectoryHelper.RunExe(app.Path);                          
                            return 0;
                        }
                        else
                        {
                            return 2;
                        }
                    }
                }
            }
            return 0;
        }

        public int AppCount(AppClass appClass)
        {
            List<App> list = appClass.AppList;
            return list.Count;
        }      
    }
    public class AppClass
    {
        private List<App> m_appList;
        private string m_name;
        private bool m_expand;
        private string m_image;
        public AppClass()
        {
            m_appList = new List<App>();
            m_name = string.Empty;
            m_expand = true;
        }
        /// <summary>
        /// 分类名称
        /// </summary>
        /// <param name="name"></param>
        public AppClass(string name, bool expand = true)
        {
            m_appList = new List<App>();
            m_name = name;
            m_expand = expand;
        }
        /// <summary>
        /// 是否展开
        /// </summary>
        public bool Expand
        {
            set { m_expand = value; }
            get { return m_expand; }
        }

        public string image
        {
            set { m_image = value; }
            get { return m_image; }
        }

        public List<App> AppList { set { m_appList = value; } get { return m_appList; } }

        public string Name { set { m_name = value; } get { return m_name; } }
    }

    public class App
    {
        private bool m_Favorite;
        private string m_name;
        private string m_path;
        public App()
        {
            m_Favorite = false;
            m_name = string.Empty;
            m_path = string.Empty;
        }

        public App(string name, string path, bool favorite = false)
        {
            m_Favorite = favorite;
            m_name = name;
            m_path = path;
        }

        public bool Favorite { set { m_Favorite = value; } get { return m_Favorite; } }
        public string Name { set { m_name = value; } get { return m_name; } }
        public string Path { set { m_path = value; } get { return m_path; } }
    }
}
